package cn.weekimwee.map3d

import android.content.res.Resources
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import cn.weekimwee.map3d.util.NaviErrorInfo
import com.amap.api.navi.AMapNavi
import com.amap.api.navi.AMapNaviListener
import com.amap.api.navi.enums.NaviType
import com.amap.api.navi.model.*
import com.autonavi.tbt.TrafficFacilityInfo
import kotlinx.android.synthetic.main.activity_navi.*

class NaviActivity : AppCompatActivity() {

    val sList:ArrayList<NaviLatLng> = arrayListOf(NaviLatLng(29.857822,121.580062))
    val eList:ArrayList<NaviLatLng> = arrayListOf(NaviLatLng(29.869331,121.565245))
    val gList:ArrayList<NaviLatLng> = arrayListOf(NaviLatLng(29.864368,121.581344))
    val TAG = "NAVI"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navi)
        aMapNaviView.onCreate(savedInstanceState)
        val aMapNavi = AMapNavi.getInstance(this)
        aMapNavi.addAMapNaviListener(object :AMapNaviListener{
            override fun onNaviInfoUpdate(p0: NaviInfo?) {
                Log.e(TAG, "总长度：" + p0?.pathRetainDistance + "米")
                Log.e(TAG, "总时间：" + p0?.pathRetainTime)
                Log.e(TAG, "当前路段:" +p0?.currentRoadName)
                Log.e(TAG, "当前路段长度：" + p0?.curStepRetainDistance + "米")
                Log.e(TAG, "当前路段时间：" + p0?.curStepRetainTime)
            }

            override fun onCalculateRouteSuccess(p0: IntArray?) {
                Log.e(TAG, "onCalculateRouteSuccess-->步行,驾车,骑行路径规划成功后的回调函数")
                aMapNavi.startNavi(NaviType.GPS)
            }

            override fun onCalculateRouteFailure(p0: Int) {
                Log.e(TAG, "onCalculateRouteFailure-->步行或者驾车路径规划失败后的回调函数:" + NaviErrorInfo.getError(p0))
            }

            override fun onServiceAreaUpdate(p0: Array<out AMapServiceAreaInfo>?) {
                Log.e(TAG, "onServiceAreaUpdate-->服务区信息回调函数" + p0.toString())
            }

            override fun onEndEmulatorNavi() {
                Log.e(TAG, "onEndEmulatorNavi-->模拟导航停止后回调函数")
            }

            override fun onArrivedWayPoint(p0: Int) {
                Log.e(TAG, "onArrivedWayPoint-->驾车路径导航到达某个途经点的回调函数:" + p0)
            }

            override fun onArriveDestination() {
                Log.e(TAG, "onArriveDestination-->到达目的地后回调函数")
            }

            override fun onPlayRing(p0: Int) {
                Log.e(TAG, "onPlayRing-->onPlayRing:" + p0)
            }

            override fun onTrafficStatusUpdate() {
                Log.e(TAG, "onTrafficStatusUpdate-->当前方路况光柱信息有更新时回调函数")
            }

            override fun onGpsOpenStatus(p0: Boolean) {
                Log.e(TAG, "onGpsOpenStatus-->用户手机GPS设置是否开启的回调函数,是否开启：" + p0)
            }

            override fun updateAimlessModeCongestionInfo(p0: AimLessModeCongestionInfo?) {
                Log.e(TAG, "updateAimlessModeCongestionInfo-->当拥堵长度大于500米且拥堵时间大于5分钟时回调")
            }

            override fun showCross(p0: AMapNaviCross?) {
                Log.e(TAG, "showCross-->显示路口放大图回调:" + p0?.picFormat)
            }

            override fun onGetNavigationText(p0: Int, p1: String?) {
                Log.e(TAG, "onGetNavigationText-->导航播报信息回调函数-->播报类型：$p0,内容：$p1")
            }

            override fun onGetNavigationText(p0: String?) {
                Log.e(TAG, "onGetNavigationText-->导航播报信息回调函数-->内容 $p0 ")
            }

            override fun updateAimlessModeStatistics(p0: AimLessModeStat?) {
                Log.e(TAG, "updateAimlessModeStatistics-->统计信息更新回调 连续5个点大于15km/h后开始回调")
            }

            override fun hideCross() {
                Log.e(TAG, "hideCross-->关闭路口放大图回调")
            }

            override fun onInitNaviFailure() {
                Log.e(TAG, "onInitNaviFailure-->初始化导航失败")
            }

            override fun onInitNaviSuccess() {
                Log.e(TAG, "onInitNaviFailure-->初始化导航成功")
            }

            override fun onReCalculateRouteForTrafficJam() {
                Log.e(TAG, "onReCalculateRouteForTrafficJam-->驾车导航时，如果前方遇到拥堵时需要重新计算路径的回调")
            }

            override fun hideLaneInfo() {
                Log.e(TAG, "hideLaneInfo-->显示道路信息回调")
            }

            override fun onNaviInfoUpdated(p0: AMapNaviInfo?) {
                Log.e(TAG,"")
            }

            override fun showModeCross(p0: AMapModelCross?) {
                Log.e(TAG,"showModeCross-->${p0.toString()}")
            }

            override fun updateCameraInfo(p0: Array<out AMapNaviCameraInfo>?) {
                Log.e(TAG, "updateCameraInfo-->导航过程中的摄像头信息回调函数:" + p0.toString())
            }

            override fun hideModeCross() {
                Log.e(TAG,"hideModeCross")
            }

            override fun onLocationChange(p0: AMapNaviLocation?) {
                Log.e(TAG, "onLocationChange-->当GPS位置有更新时的回调函数" + p0?.coord)
            }

            override fun onReCalculateRouteForYaw() {
                Log.e(TAG, "onReCalculateRouteForYaw-->步行或驾车导航时,出现偏航后需要重新计算路径的回调函数")
            }

            override fun onStartNavi(p0: Int) {
                Log.e(TAG, "onStartNavi.param-->" + p0)
            }

            override fun notifyParallelRoad(p0: Int) {
                Log.e(TAG, "notifyParallelRoad-->通知当前是否显示平行路切换")
                when (p0) {
                    0 -> Log.e(TAG, "notifyParallelRoad-->当前在主辅路过渡")
                    1 -> Log.e(TAG, "notifyParallelRoad-->当前在主路")
                    2 -> Log.e(TAG, "notifyParallelRoad-->当前在辅路")
                    else -> {
                    }
                }
            }

            override fun OnUpdateTrafficFacility(p0: AMapNaviTrafficFacilityInfo?) {
                Log.e(TAG,"")
            }

            override fun OnUpdateTrafficFacility(p0: Array<out AMapNaviTrafficFacilityInfo>?) {
                Log.e(TAG,"")
            }

            override fun OnUpdateTrafficFacility(p0: TrafficFacilityInfo?) {
                Log.e(TAG,"")
            }

            override fun showLaneInfo(p0: Array<out AMapLaneInfo>?, p1: ByteArray?, p2: ByteArray?) {
                Log.e(TAG, "showLaneInfo-->显示道路信息回调")
            }

        })
        first.setOnClickListener {
            var strategy = 0
            try {
                //再次强调，最后一个参数为true时代表多路径，否则代表单路径
                strategy = aMapNavi.strategyConvert(true, false, false, false, false)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            aMapNavi.calculateDriveRoute(sList, eList, gList, strategy)
        }
        second.setOnClickListener {
            /*var strategy = 0
            try {
                //再次强调，最后一个参数为true时代表多路径，否则代表单路径
                strategy = aMapNavi.strategyConvert(true, false, false, false, false)
            } catch (e: Exception) {
                e.printStackTrace()
            }*/
            sList.clear()
            aMapNavi.stopNavi()
            aMapNavi.calculateDriveRoute(gList, eList, sList,0)
        }
    }

    override fun getResources(): Resources {
        return baseContext.resources
    }

    override fun onResume() {
        super.onResume()
        aMapNaviView.onResume()
    }

    override fun onPause() {
        super.onPause()
        aMapNaviView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        aMapNaviView.onDestroy()
    }
}
